﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using CLFramework.Utility;

namespace StockMonitor
{
    public class LimitedStockPool
    {
        public static LimitedStockPool Instance = new LimitedStockPool();

        #region Properties

        private List<LimitedStockPoolItem> pool = new List<LimitedStockPoolItem>();

        public List<LimitedStockPoolItem> Pool
        {
            get { return pool; }
            set { pool = value; }
        }

        public List<LimitedStockPoolItem> BuyPool
        {
            get
            {
                if (pool.Count(s => s.IsBuy)<=0)
                {
                    return new List<LimitedStockPoolItem>();
                }
                return pool.Where(s => s.IsBuy).ToList();
            }
        }

        public List<LimitedStockPoolItem> SellPool
        {
            get
            {
                if (pool.Count(s => !s.IsBuy) <= 0)
                {
                    return new List<LimitedStockPoolItem>();
                }
                return pool.Where(s => !s.IsBuy).ToList();
            }
        }

        string LimitedStockPoolPath
        {
            get
            {
                return System.Windows.Forms.Application.StartupPath + System.Configuration.ConfigurationManager.AppSettings["LimitedStockPool"];
            }
        }

        #endregion

        #region Ctor.

        private LimitedStockPool()
        {
            ReLoad();
        }

        #endregion

        #region CRUD

        public bool ReLoad()
        {
            try
            {
                string txt = File.ReadAllText(this.LimitedStockPoolPath).Trim();
                if (txt == string.Empty)
                    pool = new List<LimitedStockPoolItem>();
                else
                    pool = CLFramework.SerializerUtility.XmlDeserialize<List<LimitedStockPoolItem>>(txt);

                return RefreshStockPosition();
            }
            catch (Exception e)
            {
                Log.Exception("加载股票配置失败！", e);
                return false;
            }
        }

        public bool Save()
        {
            try
            {
                string txt = CLFramework.SerializerUtility.XmlSerialize(pool).Trim();
                File.WriteAllText(this.LimitedStockPoolPath, txt);
                return true;
            }
            catch (Exception e)
            {
                //log

                Log.Exception("保存股票配置失败！", e);
                return false;
            }
        }

        public bool RefreshStockPosition()
        {
            try
            {
                //query from hs.
                var hsresult = AuthUserContext.Instance.QueryStockPosistion();

                if (hsresult == null)
                    return false;

                //remove old.
                pool.RemoveAll(s => (s.IsBuy == false) && (!hsresult.Select(h => h.stock_code).Contains(s.StockCode)));

                if (hsresult.Count == 0)
                    return true;

                //add new.
                var newhs = hsresult.Where(h => !SellPool.Select(s => s.StockCode).Contains(h.stock_code));
                foreach (var item in newhs)
                    pool.Add(new LimitedStockPoolItem() { StockCode = item.stock_code, StockName = item.stock_name, IsBuy = false });

                //update data.
                var hsdic = hsresult.ToDictionary(h => h.stock_code);
                SellPool.ForEach(s =>
                {
                    s.CurrentAmount = Convert.ToInt32(hsdic[s.StockCode].current_amount);
                    s.EnableAmount = Convert.ToInt32(hsdic[s.StockCode].enable_amount);
                });

                return true;
            }
            catch (Exception e)
            {
                Log.Exception("更新股票持仓信息时发生错误", e);
                return false;
            }
        }

        #endregion
    }
}
